package com.xhs.hawaii.purchase.service;

import com.xhs.hawaii.common.BusinessException;
import com.xhs.hawaii.purchase.mapper.PurchaseOrderSkuMapper;
import com.xhs.hawaii.purchase.model.PurchaseOrder;
import com.xhs.hawaii.purchase.model.PurchaseOrderSku;
import com.xhs.hawaii.purchase.vo.PurchaseOrderSkuReceiveVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author tangchuang
 * @version 1.0
 * @since 2018/1/26
 */
@Service
@Slf4j
public class PurchaseOrderSkuService {

    @Autowired
    private PurchaseOrderSkuMapper purchaseOrderSkuMapper;

    @Transactional
    public void receivePurchaseOrderSku(PurchaseOrder purchaseOrder, PurchaseOrderSkuReceiveVo purchaseOrderSkuVo) {
        PurchaseOrderSku purchaseOrderSku = purchaseOrderSkuMapper.getByPurchaseNoSkuCode(purchaseOrder.getPurchaseOrderNo(), purchaseOrderSkuVo.getSkuCode());
        if (purchaseOrderSku == null) {
            purchaseOrderSku = new PurchaseOrderSku();
            purchaseOrderSku.setPurchaseId(purchaseOrder.getId());
            purchaseOrderSku.setPurchaseOrderNo(purchaseOrder.getPurchaseOrderNo());
            purchaseOrderSku.setPurchaseNum(purchaseOrderSkuVo.getPurchaseNum());
            purchaseOrderSku.setPurchasePrice(purchaseOrderSkuVo.getPurchasePrice());
            purchaseOrderSku.setPurchasePriceCny(purchaseOrderSkuVo.getPurchasePriceCny());
            purchaseOrderSku.setArriveNum(purchaseOrderSkuVo.getArriveNum());
            purchaseOrderSku.setStorageTime(new Date());
            insert(purchaseOrderSku);
        } else {
            purchaseOrderSku.setArriveNum(purchaseOrderSku.getArriveNum() + purchaseOrderSkuVo.getArriveNum());
            purchaseOrderSku.setStorageTime(new Date());
            int updateNum = update(purchaseOrderSku);
            if (updateNum == 0) {
                throw new BusinessException("并发异常");
            }
        }
    }

    public int update(PurchaseOrderSku purchaseOrderSku) {
        int updateNum = purchaseOrderSkuMapper.update(purchaseOrderSku);
        if (updateNum == 1) {
            purchaseOrderSku.setVersion(purchaseOrderSku.getVersion() + 1);
        }
        return updateNum;
    }

    public void insert(PurchaseOrderSku purchaseOrderSku) {
        try {
            purchaseOrderSkuMapper.insert(purchaseOrderSku);
        } catch (DataIntegrityViolationException e) {
            log.error("采购单号,SKU code 重复", e);
            throw new BusinessException("主键重复,并发异常");
        }
    }

    public List<PurchaseOrderSku> getByPurchaseOrderId(Long purchaseOrderId){
        return purchaseOrderSkuMapper.getByPurchaseOrderId(purchaseOrderId);
    }

    public List<PurchaseOrderSku> getByPurchaseOrderNo(String purchaseOrderNo) {
        return purchaseOrderSkuMapper.getByPurchaseNo(purchaseOrderNo);
    }


}
